package com.java110.mall.acct.integral.impl;


import com.java110.dto.integral.GiftIntegralDto;
import com.java110.dto.integral.IntegralShopRuleDto;
import com.java110.intf.acct.IIntegralSettingV1InnerServiceSMO;
import com.java110.intf.acct.IIntegralShopRuleV1InnerServiceSMO;
import com.java110.mall.acct.integral.IComputeGiftIntegral;
import com.java110.utils.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 计算赠送积分
 */
@Service
public class ComputeGiftIntegralImpl implements IComputeGiftIntegral {
    private static final String MALL_DOMAIN = "MALL";


    @Autowired
    private IIntegralShopRuleV1InnerServiceSMO integralShopRuleV1InnerServiceSMOImpl;

    @Autowired
    private IIntegralSettingV1InnerServiceSMO integralSettingV1InnerServiceSMOImpl;



    @Override
    public GiftIntegralDto gift(double payMoney, String shopId) {


        IntegralShopRuleDto integralShopRuleDto = new IntegralShopRuleDto();
        integralShopRuleDto.setShopId(shopId);
        List<IntegralShopRuleDto> integralShopRuleDtos = integralShopRuleV1InnerServiceSMOImpl.queryIntegralShopRules(integralShopRuleDto);

        if (ListUtil.isNull(integralShopRuleDtos)) {
            return new GiftIntegralDto(0, 0, shopId);
        }
        IntegralShopRuleDto targetRule = null;
        for(IntegralShopRuleDto tmpIntegralShopRuleDto : integralShopRuleDtos){
            if(Double.parseDouble(tmpIntegralShopRuleDto.getMoney())< payMoney){
                targetRule = tmpIntegralShopRuleDto;
            }
        }

        if(targetRule == null){
            return new GiftIntegralDto(0, 0, shopId);
        }



        int quantity = 0;
        try {
            quantity = Integer.parseInt(targetRule.getAmount());
        }catch (Exception e){
            e.printStackTrace();
        }

        if (quantity <= 0) {
            return new GiftIntegralDto(0, 0, shopId);
        }

        double money = integralSettingV1InnerServiceSMOImpl.computeMoney(quantity);

        return new GiftIntegralDto(quantity, money, shopId,
                targetRule.getRuleId(),
                targetRule.getRuleName()
        );
    }

}
